package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.PatronsVehicleBindingData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PatronsVehicleBindingDaoImpl extends TempDataObjectDao<PatronsVehicleBindingData> {
    public static String getPatronsVehicleBindingDataSql = "select * from `t_u_patrons_vehicle_binding` where `userId`=?";
    public Map<String,PatronsVehicleBindingData> getPatronsVehicleBindingData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, PatronsVehicleBindingData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getPatronsVehicleBindingDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                PatronsVehicleBindingData data = parse(rs);
                dataMap.put(data.getVehicleId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updatePatronsVehicleBindingDataSql = "update `t_u_patrons_vehicle_binding` set patronsId=?,activityId=?,lastUpdateTime=?,systemId=?,systemEndTime=? where vehicleId=? and userId = ?";
    @Override
    protected boolean updateData(PatronsVehicleBindingData patronsVehicleBindingData) {
        patronsVehicleBindingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, patronsVehicleBindingData.getPatronsId()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(patronsVehicleBindingData.getActivityIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, patronsVehicleBindingData.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, patronsVehicleBindingData.getSystemId()));
        para.put(i++, new DbParameter(Types.BIGINT, patronsVehicleBindingData.getSystemEndTime()));

        para.put(i++, new DbParameter(Types.VARCHAR, patronsVehicleBindingData.getVehicleId()));
        para.put(i++, new DbParameter(Types.BIGINT, patronsVehicleBindingData.getUserId()));
        if (getDb().executeNoneQuery(updatePatronsVehicleBindingDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPatronsVehicleBindingDataSql = "insert into `t_u_patrons_vehicle_binding`(userId,vehicleId,patronsId,activityId,lastUpdateTime,systemId,systemEndTime) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(PatronsVehicleBindingData patronsVehicleBindingData) {
        patronsVehicleBindingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, patronsVehicleBindingData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, patronsVehicleBindingData.getVehicleId()));
        para.put(i++, new DbParameter(Types.INTEGER, patronsVehicleBindingData.getPatronsId()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(patronsVehicleBindingData.getActivityIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, patronsVehicleBindingData.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, patronsVehicleBindingData.getSystemId()));
        para.put(i++, new DbParameter(Types.BIGINT, patronsVehicleBindingData.getSystemEndTime()));
        if (getDb().executeNoneQuery(insertPatronsVehicleBindingDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PatronsVehicleBindingData parseData(ResultSet rs) throws SQLException {
        PatronsVehicleBindingData patronsVehicleBindingData = new PatronsVehicleBindingData();
        patronsVehicleBindingData.setUserId(rs.getLong("userId"));
        patronsVehicleBindingData.setVehicleId(rs.getString("vehicleId"));
        patronsVehicleBindingData.setPatronsId(rs.getInt("patronsId"));
        String str = rs.getString("activityId");
        if (StringUtils.isNullOrEmpty(str)) {
            patronsVehicleBindingData.setActivityIdList(new ArrayList<>());
        } else {
            patronsVehicleBindingData.setActivityIdList(JSON.parseArray(str, Integer.class));
        }
        patronsVehicleBindingData.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        patronsVehicleBindingData.setSystemId(rs.getInt("systemId"));
        patronsVehicleBindingData.setSystemEndTime(rs.getLong("systemEndTime"));
        return patronsVehicleBindingData;
    }
}
